<template>
	<view class="">

		<view class="active-list">
			<view class="active-title">
				<view class="title-title">标题</view>
				<u--input placeholder="请输入视频标题" v-model="title" border="none" clearable maxlength="20"></u--input>
			</view>
			<view class="active-content">
				<view class="title-title">简介</view>
				<u--textarea maxlength="50" v-model="content" class="active-content-content" border="none"
					placeholder="请输入视频简介">
				</u--textarea>
			</view>

			<view class="active-info">
				<view class="title-title">封面图</view>
				<view class="active-info-img">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deleteFile" name="1" multiple
						:maxCount="1" width="218" height="218">
						<view class="img-top">
							<img src="https://file.casugn.com/storecompcard//sbr11r9ji8lmd1bbba35f5324c1da5b910e53f3d8ac5_1681285322874.png"
								alt="">
							<text>上传图片</text>
						</view>
					</u-upload>
				</view>
			</view>

			<view class=" active-video-box">
				<view class="active-video-wrap">
					<view class="title-title">视频</view>
					<view class=" active-info-video" v-if="!videoShow">
						<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deleteFile" name="2" multiple
							:maxCount="1" accept="video">
							<view class="img-top">
								<img src="https://file.casugn.com/storecompcard//sbr11r9ji8lmd1bbba35f5324c1da5b910e53f3d8ac5_1681285322874.png"
									alt="">
								<text>上传视频</text><br />
								<text>不能超过100M</text>
							</view>
						</u-upload>
					</view>
				</view>
				<view class="video-content">
					<video v-if="videoShow" :src="videoUrl" class="video" controls></video>
				</view>

			</view>

			<view class="sign-up">
				<view class="sign-box-box">
					<u-button type="primary" color="#276AEE" text="发布视频" :loading="submitLoad" throttleTime="600"
						@click="submitActive(item,index)">
					</u-button>
				</view>

			</view>

		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	import baseConfig from '@/config/baseUrl.js'
	import {
		methods
	} from '../../uni_modules/uview-ui/libs/mixin/mixin'
	export default {

		data() {
			return {
				title: '', //标题
				content: '', //内容
				imageUrl: '', //图篇
				fileList1: [],
				fileList2: [],
				imagesUrlPath: '',
				sourceTypeIndex: 2,
				checkedValue: true,
				submitLoad: false,
				checkedIndex: 0,
				sourceType: ['拍摄', '相册', '拍摄或相册'],
				cameraList: [{
						value: 'back',
						name: '后置摄像头',
						checked: 'true'
					},
					{
						value: 'front',
						name: '前置摄像头'
					},
				],
				cameraIndex: 0,
				VideoOfImagesShow: true,
				videoUrl: '', //视频存放
				videoShow: false,
				editId: '', //当前视频id

			}
		},
		computed: {
			...mapGetters(['userInfo']),
		},
		onLoad(options) {
			if (options.info) {
				let info = JSON.parse(options.info)
				console.log(JSON.parse(options.info), '-____>')
				uni.setNavigationBarTitle({
					title: '编辑视频'
				})
				this.title = info.title
				this.content = info.remark
				this.editId = info.id
				this.imageUrl = info.cover
				this.fileList1 = info.cover.split(',').map(i => {
					return {
						url: i
					}
				})
				if (info.url) {
					this.videoUrl = info.url
					console.log(this.videoUrl, 'this.videoUrl ')
					this.videoShow = true
				}
			}

		},
		methods: {
			toText() {
				uni.navigateTo({
					url: '/pagesCard/addVideo/text'
				})
			},
			// 删除图片
			deleteFile(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				if (event.name == 1) {
					this.imageUrl = ''
					return
				}
				if (event.name == 2) {
					this.videoUrl = ''
					this.videoShow = false
					return
				}
			},
			// 提交活动
			submitActive(item, index) {
				let openId = uni.getStorageSync('openId')
				console.log("这是openId", openId)
				let userAvatar = uni.getStorageSync('userAvatar')
				console.log("这是userAvatar", userAvatar)

				if (this.title == "") {
					uni.showToast({
						title: "请输入标题",
						icon: 'none'
					});
					return false
				}
				if (this.content == "") {
					uni.showToast({
						title: "请输入视频简介",
						icon: 'none'
					});
					return false
				}
				if (this.imageUrl == "") {
					uni.showToast({
						title: "请选择封面",
						icon: 'none'
					});
					return false
				}
				if (this.videoUrl == "") {
					uni.showToast({
						title: "请选择视频",
						icon: 'none'
					});
					return false
				}
				let cover = this.fileList1.map(item => {
					return item.url
				})
				let data = {
					openId: this.userInfo.openId,
					name: this.userInfo.name,
					photo: this.userInfo.head,
					title: this.title,
					remark: this.content,
					cover: this.imageUrl,
					url: this.videoUrl,
					companyId: this.userInfo.companyId,
					cardId: this.userInfo.id,
				}
				if (this.submitLoad === true) return
				this.submitLoad = true
				if (this.editId) data.id = this.editId
				let method = this.editId ? 'editVideoSubmit' : 'addVideoSubmit'
				this.$api[method](data).then(res => {
					if (res.success) {
						this.$showToast(this.editId ? '编辑成功' : '发布成功')
						setTimeout(() => {
							this.submitLoad = false
							uni.navigateBack()
						}, 800)

					} else {
						this.submitLoad = false
						this.$showToast(res.message)
					}
				})
			},
			async afterRead(event) {
				let lists = [].concat(event.file)
				// console.log(lists, 'lists');
				let overSize = lists.some(item => item.size > 1024 * 1024 * 100)
				if (overSize) {
					uni.showToast({
						title: '上传文件超过100M',
						icon: 'none'
					})
					return
				}
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					if (result) {
						let item = this[`fileList${event.name}`][fileListLen]
						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: event.name,
							url: result,
							fileName: event.file[0].type + event.file[0].size + '',
							filePath: result
						}))
					}
					fileListLen++
				}
				//只上传一个封面 取数组第一个对象的值
				if (event.name == 1) {
					this.imageUrl = this[`fileList${event.name}`][0].url
					return
				}
				if (event.name == 2) {
					this.videoUrl = this[`fileList${event.name}`][0].filePath
					console.log(this.videoUrl, 'this.videoUrl');
					this.videoShow = true
				}
			},

			uploadFilePromise(url) {
				console.log(url)
				console.log(url)
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseConfig.uploadUrl + '/casugn-system/sys/common/upload',
						filePath: url,
						name: 'file',
						success: (res) => {
							let data = JSON.parse(res.data)
							if (data.success) {
								resolve(data.message)
							}
						},
						fail: (err) => {
							console.log(err, 'err-上传失败');
						}
					});
				})
			},










		}
	}
</script>

<style lang="scss" scoped>
	::v-deep {
		.uicon-checkmark {
			font-size: 13px !important;
			top: -4px !important;
		}

		.u-upload__deletable__icon {
			.uicon-close {
				font-size: 11px !important;
				top: 3px !important;
			}
		}
	}

	.active-list {
		background: #FFFFFF;
		// border-bottom: 1upx solid #EEEEEE;
		// padding: 41upx 30upx 100upx;
		position: relative;
		padding-bottom: calc(160rpx + env(safe-area-inset-bottom));

		.active-status {
			position: absolute;
			width: 140upx;
			height: 130upx;
			top: 38upx;
			right: 29upx;

			img {
				width: 140upx;
				height: 130upx;
			}
		}

		.active-head {
			padding: 37upx 0;
			display: flex;
			border-bottom: 1upx solid #EEEEEE;
			letter-spacing: 3upx;

			.head-img {
				width: 72upx;
				height: 72upx;
				border-radius: 50%;

				img {
					width: 72upx;
					height: 72upx;
					margin-top: 1upx;
				}
			}

			.active-name-info {
				margin-left: 21upx;

				.active-name {
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #000000;
				}

				.active-introduce {
					margin-top: 19upx;
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #999999;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					width: 400upx;
				}
			}
		}

		.active-title {
			letter-spacing: 3upx;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #666666;
			height: 160rpx;
			border-bottom: 1upx solid #EEEEEE;
			overflow: hidden;
			padding: 0 32rpx;

			.title-title {
				margin-top: 32rpx;
				margin-bottom: 16rpx;
				font-size: 26rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #1D2129;
				line-height: 36rpx;
			}

		}

		.active-content {
			padding: 0 32rpx;
			letter-spacing: 3upx;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #666666;
			border-bottom: 1upx solid #EEEEEE;
			min-height: 160rpx;

			.title-title {
				margin-top: 32rpx;
				margin-bottom: 16rpx;
				font-size: 26rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #1D2129;
				line-height: 36rpx;
			}

			::v-deep .u-textarea {
				padding: 5upx !important;
			}
		}

		.active-info {
			// border-bottom: 1upx solid #EEEEEE;
			color: #666666;
			height: 334rpx;
			padding: 0 32rpx;

			.title-title {
				margin-top: 32rpx;
				margin-bottom: 16rpx;
				font-size: 26rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #1D2129;
				line-height: 36rpx;
			}

			.active-info-img {
				margin-top: 20upx;
				width: 218rpx;
				height: 218rpx;
				background: #F7F8FA;
				position: relative;
				overflow: hidden;

				.img-top {
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%;
					text-align: center;

					img {
						display: block;
						width: 48rpx;
						height: 48rpx;
						margin: 0 auto;
						margin-top: 56rpx;
					}

					text {
						font-size: 24rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #86909C;
						line-height: 34rpx;
					}
				}

			}

		}

		.active-video-box {
			color: #666666;
			padding: 0 32rpx;
			// height: 334rpx;

			.active-video-wrap {
				.active-info-video {
					margin-top: 20upx;
					width: 218rpx;
					height: 218rpx;
					background: #F7F8FA;
					position: relative;
					overflow: hidden;

					.img-top {
						position: absolute;
						top: 0;
						left: 0;
						width: 100%;
						height: 100%;
						text-align: center;

						img {
							display: block;
							width: 48rpx;
							height: 48rpx;
							margin: 0 auto;
							margin-top: 56rpx;
						}

						text {
							font-size: 24rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #86909C;
							line-height: 34rpx;
						}
					}

				}
			}

			.video-content {
				width: 100%;
				text-align: center;
				margin-top: 20rpx;

			}

			.active-video {
				margin: 0 auto;
				margin-top: 20upx;
				width: 690upx;
				// height: 350upx;
				border-radius: 30upx;
				position: relative;
				overflow: hidden;

				img {
					width: 690upx;
					height: 350upx;
				}

			}

		}


		.avatar {
			padding: 37upx;
			flex: 1;
			display: flex;
			justify-content: space-between;

			.avatar-img {
				img {
					width: 60upx;
					height: 60upx;
					margin: 0 -15upx;
				}
			}

			.avatar-num {
				font-size: 24upx;
				font-family: PingFang SC;
				line-height: 60upx;
				font-weight: 400;
				color: #413BF3;

				.avatar-num-right {
					margin-right: 10upx;
				}
			}
		}

		.sign-up {
			// margin-top: 120upx;
			width: 100%;
			height: 160rpx;
			padding: 16rpx 32rpx;
			background-color: #fff;
			left: 0;
			position: fixed;
			bottom: 0;
			box-sizing: border-box;

			.sign-box-box {
				width: 90%;
				margin: 16rpx auto;
			}

		}

	}
</style>
